package com.sdp.wx.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sdp.core.bizc.db.Query;
import com.sdp.core.bizc.vo.DataCenter;
import com.sdp.core.bizc.vo.Result;
import com.sdp.wx.db.wx_emp_attendance.entity.WxEmpAttendance;
import com.sdp.wx.db.wx_emp_attendance.service.IWxEmpAttendanceService;
import com.sdp.wx.db.wx_emp_attendance_result.entity.WxEmpAttendanceResult;
import com.sdp.wx.db.wx_emp_attendance_result.service.IWxEmpAttendanceResultService;
import com.sdp.wx.db.wx_emp_work.entity.WxEmpWork;
import com.sdp.wx.db.wx_emp_work.service.IWxEmpWorkService;
import com.sdp.wx.db.wx_emp_work_attendance_rule.entity.WxEmpWorkAttendanceRule;
import com.sdp.wx.db.wx_emp_work_attendance_rule.service.IWxEmpWorkAttendanceRuleService;
import com.sdp.wx.utils.LocalDateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.Duration;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zsp
 * @since 2024-02-19
 */
@RestController
@RequestMapping("/WxEmpAttendanceController")
public class WxEmpAttendanceController {

    @Autowired
    private IWxEmpAttendanceService iWxEmpAttendanceService;

    @Autowired
    private IWxEmpWorkAttendanceRuleService iWxEmpWorkAttendanceRuleService;
    @Autowired
    private IWxEmpAttendanceResultService iWxEmpAttendanceResultService;
    @Autowired
    private IWxEmpWorkService iWxEmpWorkService;
    @Autowired
    private Query query;

    @PostMapping(value = "/page")
    public Result<?> page(@RequestBody DataCenter dataCenter) {
        return Result.ok(query.page(iWxEmpAttendanceService, WxEmpAttendance.class, dataCenter));
    }
    @PostMapping(value = "/save")
    @Transactional(rollbackFor=Exception.class)
    public Result<?> save(@RequestBody WxEmpAttendance wxEmpAttendance) {
        if(wxEmpAttendance==null) {
            return Result.error("信息为空");
        }
        if(StringUtils.isEmpty(wxEmpAttendance.getKeyId())) {

            iWxEmpAttendanceService.save(wxEmpAttendance);
            saveEmpAttendanceResult(wxEmpAttendance);
        }else {

            iWxEmpAttendanceService.updateById(wxEmpAttendance);
        }

        return Result.ok();
    }

    /**
     * 保存打卡结果
     * @param wxEmpAttendanc
     */
    public  void saveEmpAttendanceResult(WxEmpAttendance wxEmpAttendanc){
        QueryWrapper<WxEmpWork> queryWrapper=new QueryWrapper<>();
        queryWrapper.lambda().eq(WxEmpWork::getWorkEmpNo,wxEmpAttendanc.getEmpNo());
        WxEmpWork wxEmpWork=iWxEmpWorkService.getOne(queryWrapper);
        if(wxEmpWork!= null){
            // 查询规则
            QueryWrapper<WxEmpWorkAttendanceRule> empWorkAttendanceRuleQueryWrapper=new QueryWrapper<>();
            empWorkAttendanceRuleQueryWrapper.lambda().eq(WxEmpWorkAttendanceRule::getWorkAttendanceId,wxEmpWork.getWorkAttendanceId());
            List<WxEmpWorkAttendanceRule> wxEmpWorkAttendanceRuleList=iWxEmpWorkAttendanceRuleService.list(empWorkAttendanceRuleQueryWrapper);
            if(wxEmpWorkAttendanceRuleList.size()>0){
                for(WxEmpWorkAttendanceRule wxEmpWorkAttendanceRule:wxEmpWorkAttendanceRuleList){
                    // 根据打卡时间，判断是上班还是下班打卡
                    String clockInTime=LocalDateUtil.localDateTime2String(wxEmpAttendanc.getClockInTime()).substring(0,11);
                    // 上班开始打卡时间
                    LocalDateTime workOnStartTime= LocalDateUtil.String2LocalDateTime(clockInTime+wxEmpWorkAttendanceRule.getWorkOnStartTime());
                    // 上班结束打卡时间
                    LocalDateTime workOnEndTime= LocalDateUtil.String2LocalDateTime(clockInTime+wxEmpWorkAttendanceRule.getWorkOnEndTime());

                    // 下班开始打卡时间
                    LocalDateTime workComeStartTime= LocalDateUtil.String2LocalDateTime(clockInTime+wxEmpWorkAttendanceRule.getWorkComeStartTime());
                    // 下班结束打卡时间
                    LocalDateTime workComeEndTime= LocalDateUtil.String2LocalDateTime(clockInTime+wxEmpWorkAttendanceRule.getWorkComeEndTime());

                    // 上班时间
                    LocalDateTime workOnTime= LocalDateUtil.String2LocalDateTime(clockInTime+wxEmpWorkAttendanceRule.getWorkOnTime());
                    // 下班时间
                    LocalDateTime workComeTime= LocalDateUtil.String2LocalDateTime(clockInTime+wxEmpWorkAttendanceRule.getWorkComeTime());  // 上班打卡，判断打卡时间是否迟到
                    WxEmpAttendanceResult wxEmpAttendanceResult=new WxEmpAttendanceResult();
                    wxEmpAttendanceResult.setOrgNo(wxEmpAttendanc.getOrgNo());
                    wxEmpAttendanceResult.setEmpName(wxEmpAttendanc.getEmpName());
                    wxEmpAttendanceResult.setEmpNo(wxEmpAttendanc.getEmpNo());
                    wxEmpAttendanceResult.setClockInTime(wxEmpAttendanc.getClockInTime());// 打卡时间
                    wxEmpAttendanceResult.setAttendanceTime(workOnTime);// 考勤时间
                    if(wxEmpAttendanc.getClockInTime().compareTo(workOnStartTime)>=0 && wxEmpAttendanc.getClockInTime().compareTo(workOnEndTime)<=-1 ){
                        if(wxEmpAttendanc.getClockInTime().compareTo(workOnTime)>0){
                            wxEmpAttendanceResult.setAttendanceResult("迟到");
                            Duration duration = Duration.between(workOnTime,wxEmpAttendanc.getClockInTime());
                            wxEmpAttendanceResult.setAttendanceBeLate(String.valueOf(duration.toMinutes()));
                        }
                        wxEmpAttendanceResult.setAttendanceType("1");
                        iWxEmpAttendanceResultService.save(wxEmpAttendanceResult);
                    }
                    if(wxEmpAttendanc.getClockInTime().compareTo(workComeStartTime)>=0 && wxEmpAttendanc.getClockInTime().compareTo(workComeEndTime)<=-1 ){
                        if(wxEmpAttendanc.getClockInTime().compareTo(workComeTime)<0){
                            wxEmpAttendanceResult.setAttendanceResult("早退");
                            Duration duration = Duration.between(workOnTime,wxEmpAttendanc.getClockInTime());
                            wxEmpAttendanceResult.setAttendanceBeLate(String.valueOf(duration.toMinutes()));
                        }
                        wxEmpAttendanceResult.setAttendanceType("2");
                        iWxEmpAttendanceResultService.save(wxEmpAttendanceResult);
                    }
                }
            }
        }
    }
}

